package com.ouc.tcp.test;

import com.ouc.tcp.client.Client;
import com.ouc.tcp.message.TCP_PACKET;

import java.util.Vector;

public class ReceiveWindow_SR extends Window {

    public ReceiveWindow_SR(Client client) {
        super(client);
    }

    public Vector<TCP_PACKET> recvPacket(TCP_PACKET packet) {
        Vector<TCP_PACKET> myvector = new Vector<TCP_PACKET>();
        int seq = packet.getTcpH().getTh_seq();
        int index = seq % size;
        if(index >= 0) {
            isAck[index] = true;
            packets[index] = packet;
            if(seq == base) {//收到的包是窗口的第一个包
                int i;
                for(i = base; i <= end && isAck[i % size]; i++) {
                    myvector.addElement(packets[i % size]);
                    isAck[i % size] = false;
                    packets[i % size] = null;
                }
              //移动窗口
                base = i;
                end = base + size - 1;
            }
        }
        return myvector;
    }
}
